<template>
  <el-card>
    <!-- 面包屑 -->
    <my-bread leavl1="商品管理" leavl2="分类参数"></my-bread>
    <!-- 提示 -->
    <el-alert
      class="alert"
      title="只允许为第三级分类设置参数"
      type="error"
      :closable="false"
    >
    </el-alert>

    <!-- 级联选择器 -->
    <el-form class="formcas" label-position="left" label-width="80px">
      <el-form-item label="商品分类">
        <!-- 级联选择器 -->
        <el-cascader
          clearable
          expand-trigger="hover"
          v-model="selectedOptions"
          :options="options"
          :props="defaultProp"
          @change="handleChange()"
        ></el-cascader>
      </el-form-item>

      <!-- tab -->
      <el-tabs v-model="active" type="card" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="1">
          <el-button type="danger" plain> 设置动态参数</el-button>
          <!-- 展开行 -->
          <el-table :data="arrDyparams" style="width: 100%">
            <el-table-column type="expand" label="#">
              <template slot-scope="scope">
                <!-- el-tag -->
                <el-tag
                  :key="tag"
                  v-for="tag in scope.row.attr_vals"
                  closable
                  :disable-transitions="false"
                  @close="
                    handleClose(
                      scope.row.attr_vals,
                      scope.row.attr_name,
                      scope.row.attr_id,
                      tag
                    )
                  "
                >
                  {{ tag }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="inputVisible"
                  v-model="inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="
                    handleInputConfirm(
                      scope.row.attr_vals,
                      scope.row.attr_name,
                      scope.row.attr_id
                    )
                  "
                  @blur="
                    handleInputConfirm(
                      scope.row.attr_vals,
                      scope.row.attr_name,
                      scope.row.attr_id
                    )
                  "
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column label="属性名称" prop="attr_name">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  plain
                  type="primary"
                  icon="el-icon-edit"
                  circle
                  @click="showEditUserDia(scope.row)"
                ></el-button>
                <el-button
                  @click="showDeleteMsgBox(scope.row.id)"
                  size="mini"
                  plain
                  type="danger"
                  icon="el-icon-delete"
                  circle
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态参数" name="2">
          <el-button type="danger"> 设置静态参数</el-button>
          <el-table :data="arrStaticparams" style="width: 100%">
            <el-table-column type="index" label="#"> </el-table-column>
            <el-table-column label="属性名称" prop="attr_name">
            </el-table-column>
            <el-table-column label="属性值" prop="attr_vals"> </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  plain
                  type="primary"
                  icon="el-icon-edit"
                  circle
                  @click="showEditUserDia(scope.row)"
                ></el-button>
                <el-button
                  @click="showDeleteMsgBox(scope.row.id)"
                  size="mini"
                  plain
                  type="danger"
                  icon="el-icon-delete"
                  circle
                ></el-button>
              </template>
            </el-table-column> </el-table
        ></el-tab-pane>
      </el-tabs>
    </el-form>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      active: "1",
      //级联选择器绑定的数据
      options: [],
      selectedOptions: [],
      defaultProp: {
        label: "cat_name",
        value: "cat_id",
        children: "children",
      },
      list: [],
      //动态参数的数据数组
      arrDyparams: [],
      //静态参数的数据数组
      arrStaticparams: [],

      //tag数据
      inputVisible: false,
      inputValue: "",
    };
  },
  created() {
    this.getGoodCate();
  },
  methods: {
    async handleClick() {
      if (this.active === "2") {
        if (this.selectedOptions.length === 3) {
          const res = await this.$http.get(
            `categories/${this.selectedOptions[2]}/attributes?sel=only`
          );

          this.arrStaticparams = res.data.data;
        }
      }
    },
    //点击X按钮关闭
    async handleClose(attr_vals, attr_name, attr_id, tag) {
      attr_vals.splice(attr_vals.indexOf(tag), 1);
      let putData = {
        attr_name: attr_name,
        attr_vals: attr_vals.join(","),
        attr_sel: "many",
      };
      const res = await this.$http.put(
        `categories/${this.selectedOptions[2]}/attributes/${attr_id}`,
        putData
      );
      console.log(res);
    },
    //点击newtag+按钮
    async showInput(row) {
      this.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    // 回车键失去焦点
    async handleInputConfirm(attr_vals, attr_name, attr_id) {
      let inputValue = this.inputValue;
      if (inputValue) {
        attr_vals.push(inputValue);

        //发送请求
        var attr_sel;
        let putData = {
          attr_name: attr_name,
          attr_sel: "many",
          attr_vals: attr_vals.join(","),
        };
        const res = await this.$http.put(
          `categories/${this.selectedOptions[2]}/attributes/${attr_id}`,
          putData
        );
        console.log(res);
      }
      this.inputVisible = false;
      this.inputValue = "";
    },

    //级联选择器
    async handleChange() {
      if (this.selectedOptions.length === 3) {
        //动态获取参数
        const res = await this.$http.get(
          `categories/${this.selectedOptions[2]}/attributes?sel=many`
        );

        this.arrDyparams = res.data.data;
        this.arrDyparams.forEach((item) => {
          item.attr_vals =
            item.attr_vals.length === 0 ? [] : item.attr_vals.trim().split(",");
        });
      }
    },
    //获取三级级联分类数据
    async getGoodCate() {
      const res = await this.$http.get(`categories?type=3`);
      // console.log(res);
      this.options = res.data.data;
    },
  },
};
</script>

<style>
.alert {
  margin-top: 30px;
}
.formcas {
  margin-top: 30px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>